package com.javasearch;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.JarClassLoader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javasearch.index.DelIndex;
import com.javasearch.index.JarIndex;
import com.javasearch.index.build.JarSourceBuild;
import com.javasearch.index.build.JavaBuild;

import com.javasearch.index.build.PomXmlBuild;
import com.javasearch.search.ClassSearch;
import com.javasearch.search.JarSourcesSearch;
import com.javasearch.search.JavaSearch;
import com.javasearch.utils.ExecutorUtils;
import com.javasearch.utils.ReadFileLineUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import java.io.File;
import java.util.concurrent.ExecutorService;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MainApplication {
    private static final Logger logger = LoggerFactory.getLogger(MainApplication.class);

    //一个jar包平级大概也就100~2000左右的类,取个平均值1000 ,一个项目顶天也就500左右个jar包,所以这里的线程池队列大小设置为500*1000=500000/1000=500,为了保证万无一失,500*1000=500000/100=5000
    public static ExecutorService executor = ExecutorUtils.getExecutorCustom("MainApplication", 500 * 1000, 5000);
    public static JarClassLoader jarClassLoader = new JarClassLoader();


    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(MainApplication.class);
        //去掉springboot的banner
        springApplication.setBannerMode((Banner.Mode.OFF));
        //设置为非web项目,不会启动tomcat
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.run(args);

        Config config = null;
        String path = System.getProperty("Config_f");
        if (path != null && !path.isEmpty()) {
            config = config(path);
        }
        String path1 = System.getProperty("config_s");
        if (path1 != null && !path1.isEmpty()) {
            config = configJson(path);
        }
        if (config == null) {
            logger.error("参数只能是，-DConfig_f=xxx 或者 -DConfig_s=xxx");
            System.exit(0);
        }
        if (config.getProjectPath().isEmpty()) {
            logger.error("project-path 不能是空的");
            System.exit(0);
        }
        //如果index和search的activate都是true,那么报错,因为这两个不能同时开启
        if (config.getIndex().isActivate() && config.getSearch().isActivate()) {
            logger.error("index和search的activate不能同时开启");
            System.exit(0);
        }
        if (config.getIndex().isActivate()){ //如果开启了索引机制
            if (config.getIndex().isRefresh()){//如果开启了索引刷新机制
                DelIndex.delIndex();
            }
            Index(config);//执行构建索引机制
        }
        if (config.getSearch().isActivate()){//如果开启了搜索机制
            //备份旧的搜索结果
            String searchPath = config.getSearch().getSearchPath();
            File file = new File(searchPath);
            if (file.exists()){
                FileUtil.copyFile(file, new File(searchPath+"_"+System.currentTimeMillis()));
                //删除旧的搜索结果
                FileUtil.del(file);
            }
            Search(config);//执行搜索机制
        }

        logger.info("=================执行完毕=================");
        System.exit(0);
    }

    public static Config config(String path) {
        File file = new File(path);
        String context = ReadFileLineUtil.readFileStrAll(file);
        Config config1 = configJson(context);
        return config1;
    }

    private static Config configJson(String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String projectPath = (String) jsonObject.get("project-path");
        Config config = new Config();
        config.setProjectPath(projectPath);
        Object index = jsonObject.get("index");
        if (index != null) {
            Config.Index index1 = JSON.parseObject(index.toString(), Config.Index.class);
            config.setIndex(index1);
        }
        Object search = jsonObject.get("search");
        if (search != null) {
            Config.Search search1 = JSON.parseObject(search.toString(), Config.Search.class);
            config.setSearch(search1);
        }

        return config;
    }

    public static void Index(Config config) {
        Config.Index index = config.getIndex();
        if (index.isJavaFile()) {
            JavaBuild.writePath(config.getProjectPath());
        }
        if (index.isJarSource()) {
            JarSourceBuild.writePath(config.getProjectPath());
        }
        if (index.isPom()) {
            PomXmlBuild.writePath(config);
        }
        if (index.isJar()) {
            JarIndex.buildIndex(config.getProjectPath());
        }

    }

    public static   void  Search(Config config){
        //搜索java文件
        JavaSearch.search(config);
        //搜索jar包源码
        JarSourcesSearch.search(config);
        //搜索jar包
        ClassSearch.search(config);
    }


}